package com.example.binding_recyclerview.adapter;


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableArrayList;
import androidx.recyclerview.widget.RecyclerView;

import com.example.binding_recyclerview.R;
import com.example.binding_recyclerview.databinding.MyListItemBinding;
import com.example.binding_recyclerview.model.Student;
import com.example.binding_recyclerview.myInter.OnItemClick;

public class MyRecycleAdapter extends RecyclerView.Adapter<MyRecycleAdapter.MyViewHlder> {

    //viewholder 用于实例化itemview视图
    public class MyViewHlder extends RecyclerView.ViewHolder {

        MyListItemBinding listItemBinding;

        public MyViewHlder(@NonNull View itemView) {
            super(itemView);
            listItemBinding = DataBindingUtil.bind(itemView);
        }
    }

    ObservableArrayList<Student> dataList;

    OnItemClick<Student> onItemClick;

    //构造
    public MyRecycleAdapter(ObservableArrayList<Student> dataList) {
        this.dataList = dataList;

    }

    public void setOnItemClick(OnItemClick<Student> onItemClick) {
        this.onItemClick = onItemClick;
    }

    @NonNull
    @Override
    public MyRecycleAdapter.MyViewHlder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //通过id拿到视图
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View root = inflater.inflate(R.layout.my_list_item, parent, false);
        //通过binding对象拿到试图
        //MyListItemBinding binding = MyListItemBinding.inflate(inflater,parent,false);
        //View view = binding.getRoot();


        MyViewHlder myViewHlder = new MyViewHlder(root);

        //绑定事件
        myViewHlder.listItemBinding.id.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = myViewHlder.getLayoutPosition();
                //Toast.makeText(view.getContext(),dataList.get(position).getId(), Toast.LENGTH_SHORT ).show();
                setEditText(myViewHlder);
            }
        });

        //点击之后让右侧碎片修改内容
        myViewHlder.listItemBinding.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setEditText(myViewHlder);
            }
        });

        return myViewHlder;
    }

    //为students中的每个元素设置view
    @Override
    public void onBindViewHolder(@NonNull MyRecycleAdapter.MyViewHlder holder, int position) {
        Student student = dataList.get(position);
        holder.listItemBinding.setStudent(student);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public void setEditText(MyViewHlder myViewHlder) {
        int position = myViewHlder.getLayoutPosition();
        Student student = dataList.get(position);
        onItemClick.click(student);
    }


}